package org.hhchat.leetcode.dfs;

import java.util.Arrays;
import java.util.HashSet;

public class code695_easy {

    static class Solution {
        int max = Integer.MIN_VALUE;
        public int maxAreaOfIsland(int[][] grid) {
            int lenX = grid[0].length;
            int lenY = grid.length;
            int count = 0;
            HashSet<String> set = new HashSet<>();
            boolean[][] isVisited = new boolean[lenY][lenX];
            for(int i=0;i<lenY;i++) {
                for(int j = 0;j<lenX;j++) {
                    if (grid[i][j] == 1 && !isVisited[i][j]) {
                        stringBuilder = new StringBuilder();
                        dfs(grid, isVisited, j, i ,'c');
                        System.out.println("find a new one ["+stringBuilder.toString()+"] is the number of " + ++count);
                        set.add(stringBuilder.toString());
                    }
                }
            }
            return set.size();
        }

        StringBuilder stringBuilder;

        public void dfs(int[][] grid,boolean[][] isVisited, int x, int y , char d) {
            if (x < 0 || x >= grid[0].length) {
                stringBuilder.append("|");
                return ;
            }
            if (y < 0 || y >= grid.length) {
                stringBuilder.append("|");
                return;
            }
            if (grid[y][x] == 0 || isVisited[y][x]) {
                stringBuilder.append("|");
                return;
            }
            isVisited[y][x] = true;
            stringBuilder.append(d);
            dfs(grid, isVisited, x - 1, y,'l');
            dfs(grid, isVisited, x +1, y,'r');
            dfs(grid, isVisited, x, y - 1,'u');
            dfs(grid, isVisited, x, y + 1, 'd');
        }
    }

    public static void main(String[] args) {
        int[][] grid = new int[4][];
        grid[0] = new int[]{0,0,1,0,1,0,1,1,1,0,0,0,0,1,0,0,1,0,0,1,1,1,0,1,1,1,0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,0,0,0,1,1,1,1,0};
        grid[1] = new int[]{0,0,1,0,0,1,1,1,0,0,1,0,1,0,0,1,1,0,0,1,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,1,1,0,1,0,0,0};
        grid[2] = new int[]{0,1,0,1,0,1,1,1,0,0,1,1,0,0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,0,1,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,1,0};
        grid[3] = new int[]{1,0,1,0,0,1,0,1,0,0,1,0,0,1,1,1,0,1,0,0,0,0,1,0,1,0,0,1,0,1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1};
        printDArr(grid);
        System.out.println(new Solution().maxAreaOfIsland(grid));
    }

    public static void printDArr(int[][] grid) {
        for (int g[] : grid) {
            System.out.println(Arrays.toString(g));
        }
        System.out.println();
    }
}
